package com.example.database.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @author zpMeng
 * @since 10/10/2025
 */
@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceConfig {


    @Bean("evictDataSource")
    @Primary
    @ConfigurationProperties("spring.datasource")
    public EvictableDataSource dataSource(DataSourceProperties properties) {
        HikariDataSource build = properties.initializeDataSourceBuilder()
                .type(HikariDataSource.class) // 或其他连接池
                .build();
        return new EvictableDataSource(build);
    }

    @Bean
    public JdbcTemplate jdbcTemplate(@Qualifier("evictDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}

